package com.woniu.dao;

import com.woniu.entity.Course;
import com.woniu.entity.Order;
import com.woniu.entity.PageResult;
import org.apache.ibatis.annotations.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderDao {



    @Insert("INSERT INTO collect_user_course (uid, cid) \n" +
            "VALUES (#{uid}, #{cid});")
    void addsc(@Param("uid") Integer userId, @Param("cid") Integer courseId);

    @Select("SELECT uid FROM collect_user_course WHERE cid = #{cid} AND uid = #{uid}")
    Integer selectUidByCid(@Param("uid") Integer userId, @Param("cid") Integer courseId);

    @Delete("DELETE FROM collect_user_course WHERE cid = #{cid} AND uid = #{uid}")
    void delectsc(@Param("uid") Integer userId, @Param("cid") Integer courseId);

    List<Order> queryAllByLimit(
            @Param("orderMethod") String orderMethod,
            @Param("status") Integer status,
            @Param("userId") Integer userId,
            @Param("username") String username,
            @Param("pageable") PageResult<Order> pageable
    );

    Long count(
            @Param("orderMethod") String orderMethod,
            @Param("status") Integer status,
            @Param("username") String username,
            @Param("userId") Integer userId
    );
    // 新增更新备注方法
    int updateRemark(@Param("orderId") String orderId, @Param("remark") String remark);

    // 总销售额
    BigDecimal getTotalSales();

    // 订单总数
    Long getOrderCount();

    // 待支付订单总数
    Long getPendingOrderCount();

    // 支付用户数
    Long getPayingUserCount();

    // 每日销售趋势
    List<Map<String, Object>> getDailySalesTrend(@Param("startDate") Date startDate, @Param("endDate") Date endDate);

//    @Select("SELECT " +
//            "FLOOR(HOUR(pay_time)/2)*2 AS hour_interval, " +
//            "SUM(price) AS total_amount " +
//            "FROM `order` " +
//            "WHERE status = 0 " +
//            "AND DATE(pay_time) = DATE(DATE_SUB(NOW(), INTERVAL 1 DAY)) " +
//            "GROUP BY hour_interval " +
//            "ORDER BY hour_interval")
    List<Map<String, Object>> getYesterdayPaymentTrend();

    // 插入单个订单
    @Insert("INSERT INTO `order` (order_id, orderNo,price,remark, order_method, status, course_name,create_time, update_time) " +
            "VALUES(#{orderId}, #{orderNo},#{price},#{remark}, #{orderMethod}, #{status},#{courseName}, #{createTime}, #{updateTime})")
    int insertOrder(Order order);

    // 根据订单编号查询订单列表
    @Select("SELECT * FROM order WHERE category_id = #{category_id}")
    List<Order> selectOrdersByOrderNo(String orderNo);

    // 批量更新订单状态
    @Update("UPDATE order SET status = #{status}, update_time = #{updateTime} WHERE category_id = #{category_id}")
    int updateOrdersStatus(@Param("category_id") String orderNo,
                           @Param("status") Integer status,
                           @Param("updateTime") Date updateTime);

    //支付榜
    List<Map<String, Object>> getPaymentRankings();

    //获取订单详情
    @Select("SELECT o.*, u.username as username, u.real_name as userRealName " +
            "FROM `order` o LEFT JOIN `user` u ON o.user_id = u.user_id " +
            "WHERE o.order_id = #{orderId}")
    Order getOrderById(@Param("orderId") String orderId);

    @Select("SELECT c.*, u.username as teacherName, u.real_name as teacherRealName " +
            "FROM course c " +
            "JOIN order_course oc ON c.id = oc.course_id " +
            "LEFT JOIN `user` u ON c.teacher_id = u.user_id " +
            "WHERE oc.order_id = #{orderId}")
    List<Course> getCoursesByOrderId(@Param("orderId") String orderId);

 @Select("SELECT *  FROM user_course WHERE course_id = #{cid} AND user_id = #{uid}")
    Integer selectUC(@Param("uid") Integer userId, @Param("cid") Integer courseId);


    @Select("SELECT course.*\n" +
            "FROM user_course\n" +
            "-- 内连接 course 表，通过 course_id 和 course 表的 id 关联\n" +
            "JOIN course ON user_course.course_id = course.id\n" +
            "-- 根据 user_id 筛选\n" +
            "WHERE user_course.user_id = #{userId};")
    List<Course> selectOrder(Integer userId);

    @Delete("delete from `order` where order_id=#{orderId}")
    void deleteOrder(String orderId);

    @Delete("delete from order_course where order_id=#{orderId}")
    void deleteOrderAndCourse(String orderId);
}
